package com.blacktry.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blacktry.entity.enums.OprLogConst;
import com.blacktry.entity.model.ApiResult;
import com.blacktry.entity.model.Admin;
import com.blacktry.entity.model.request.PasswordRequest;
import com.blacktry.entity.model.request.UserRequest;
import com.blacktry.entity.model.vueVO.Option;
import com.blacktry.service.AdminService;
import com.blacktry.utils.ApiResultHandler;
import com.blacktry.utils.CurrentUserUtil;
import com.blacktry.utils.log.OperLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.bind.DefaultValue;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author blackTry
 * @since 2021-06-23
 */
@Api(value="用户类模块", tags = "用户管理接口")
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @OperLog(operModul = "用户管理-添加用户",operType = OprLogConst.ADD,operDesc = "添加用户数据功能")
    @ApiOperation("添加用户数据")
    @PostMapping("/insert")
    public ApiResult<Integer> insert(@RequestBody Admin admin) {
        return ApiResultHandler.buildApiResult(200, "成功", adminService.insert(admin));
    }

    @OperLog(operModul = "用户管理-查找用户",operType = OprLogConst.SELECT,operDesc = "根据用户名查找用户数据功能")
    @ApiOperation("根据用户名查找用户数据")
    @GetMapping("/select/{username}")
    public ApiResult<Admin> selectByUsername(@PathVariable String username){
        return ApiResultHandler.buildApiResult(200, "成功", adminService.selectByUsername(username));
    }

    @OperLog(operModul = "用户管理-判断密码是否正确",operType = OprLogConst.SELECT,operDesc = "判断密码是否正确功能")
    @ApiOperation("判断密码是否正确")
    @GetMapping("/select/{id}/{password}")
    public ApiResult<Admin> selectByUsername(@PathVariable("id") Integer id,@PathVariable("password") String password){
        return ApiResultHandler.buildApiResult(200,"成功",adminService.isRightPassword(id,password));
    }

   @OperLog(operModul = "用户管理-插入或更新用户",operType = OprLogConst.ADD,operDesc = "根据用户名插入或更新数据功能")
   @ApiOperation("根据用户名插入或更新数据")
   @PostMapping("/insertOrUpdate")
   public ApiResult<Integer> insertOrUpdate(@RequestBody Admin admin){
       return ApiResultHandler.buildApiResult(200,"成功",adminService.insertOrUpdate(admin));
   }

    @OperLog(operModul = "用户管理-分页查看所有用户",operType = OprLogConst.SELECT,operDesc = "分页查看所有用户功能")
    @ApiOperation("分页查看所有用户")
    @PostMapping("/findAll/{page}/{size}")
    public ApiResult<Page<Admin>> findAll(@RequestBody UserRequest userRequest, @DefaultValue("1") @PathVariable("page") Integer page, @DefaultValue("6") @PathVariable("size") Integer size){
        System.out.println("根据name,username,affiliation,competent,enable查询用户的所有信息");
        Page<Admin> adminPage = new Page<>(page,size);
        return ApiResultHandler.buildApiResult(200, "请求成功！", adminService.selectPage(userRequest,adminPage));
    }

    @OperLog(operModul = "用户管理-更改用户的状态",operType = OprLogConst.UPDATE,operDesc = "更改用户的状态功能")
    @ApiOperation("更改用户的状态")
    @PutMapping("/updateEnabled")
    public ApiResult<Integer> enabled(@RequestParam("id") Integer id,@RequestParam("enabled") Integer enabled){
        return ApiResultHandler.buildApiResult(200,"成功",adminService.updateEnabled(id,enabled));
    }

    @OperLog(operModul = "用户管理-批量删除用户",operType = OprLogConst.DELETE,operDesc = "批量删除用户功能")
    @ApiOperation("批量删除用户")
    @DeleteMapping("/deleteBatchId")
    public ApiResult<Integer> deleteBatchId(String[] ids){
        return ApiResultHandler.buildApiResult(200,"成功",adminService.deleteBatchId(ids));
    }

    @OperLog(operModul = "用户管理-获取登陆用户methods测试版",operType = OprLogConst.SELECT,operDesc = "获取登陆用户methods测试版功能")
    @ApiOperation("获取登陆用户methods测试版")
    @GetMapping("/getCurrentUser")
    public ApiResult<Admin> getCurrentUser(){
        return ApiResultHandler.buildApiResult(200,"成功", CurrentUserUtil.getCurrentUser());
    }

    @OperLog(operModul = "用户管理-更换头像",operType = OprLogConst.UPDATE,operDesc = "用户更换头像功能")
    @ApiOperation("用户更换头像")
    @PostMapping("/updateFace")
    public ApiResult updateFace(MultipartFile file,String username){
        return ApiResultHandler.buildApiResult(200,"成功",adminService.updateFace(file,username));
    }

    @OperLog(operModul = "用户管理-修改密码",operType = OprLogConst.UPDATE,operDesc = "用户修改密码功能")
    @ApiOperation("修改密码")
    @PutMapping("/updatePassword")
    public Boolean updatePassword(@RequestBody PasswordRequest passwordRequest){
        return adminService.updatePassword(passwordRequest);
    }

    @OperLog(operModul = "用户管理-所有归属",operType = OprLogConst.SELECT,operDesc = "用户所有归属")
    @ApiOperation("查看用户所有归属")
    @GetMapping("/selectAllAffiliation")
    public String[] selectAllAffiliation(){
        return adminService.selectAllAffiliation();
    }

    @OperLog(operModul = "用户管理-查看教师身份列表",operType = OprLogConst.SELECT,operDesc = "查看教师身份列表")
    @ApiOperation("查看教师身份列表")
    @GetMapping("/selectAllTeacher")
    public List<Option> selectAllTeacher(){
        return adminService.selectAllTeacher();
    }
}

